package com.settlement.system.common.util;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;

public class DateUtils {

    public static final DateTimeFormatter crossDf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
    static final DateTimeFormatter diagonalDf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
    static final DateTimeFormatter version_df = DateTimeFormatter.ofPattern("yyyyMMddHHmmssSSS");

    public static LocalDateTime convert(String date) {
        try {
            if(!StringUtils.hasText(date)){
                return null;
            }
            if (date.length() < 17) {
                date = "%s:00".formatted(date);
            }
            if (date.contains("T")) {
                date = StringUtils.replaceStr(date, "T", " ");
            }
            if (date.contains("-")) {
                return LocalDateTime.parse(date, crossDf);
            } else if (date.contains("/")) {
                return LocalDateTime.parse(date, diagonalDf);
            } else {
                return null;
            }
        } catch (Exception e) {
            return null;
        }
    }

    public static String getDateString() {
        return LocalDateTime.now().format(version_df);
    }

    public static String getDateString(DateTimeFormatter df) {
        return LocalDateTime.now().format(df);
    }

    public static String getDateString(LocalDateTime dateTime, DateTimeFormatter df) {
        return dateTime.format(df);
    }

    public static LocalDate convertLocalDate(String date, String pattern) {
        try {
            DateTimeFormatter df = DateTimeFormatter.ofPattern(pattern);
            return LocalDate.parse(date, df);
        } catch (Exception e) {
            return null;
        }
    }

    public static String formatDate(LocalDateTime date) {
        try {
            DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyyMM");
            return date.format(df);
        } catch (Exception e) {
            return null;
        }
    }

    public static List<String> getBillDateList(String billMonth) {
        List<String> dateList = new ArrayList<>();
        dateList.add(billMonth);
        DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyyMMdd");
        LocalDate localDateTime = LocalDate.parse("%s01".formatted(billMonth), df);
        for (int i = 1; i < 4; i++) {
            dateList.add(formatDate(localDateTime.plusMonths(i).atStartOfDay()));
        }
        for (int i = 1; i < 4; i++) {
            dateList.add(formatDate(localDateTime.minusMonths(i).atStartOfDay()));
        }
        return dateList;
    }

    // 通过此方法，将字符串的日期格式化
    public static LocalDateTime convertStringToLocalDateTime(String text) {
        try {
            String[] dateArr = text.split(" ");
            String dateStr = dateArr[0];
            String timeStr = dateArr[1];
            String year = null;
            String month = null;
            String day = null;
            dateStr = dateStr.replaceAll("/", "-");
            if (dateStr.contains("-")) {
                String[] dateSplit = dateStr.split("-");
                year = dateSplit[0];
                month = dateSplit[1];
                day = dateSplit[2];
                if (month.length() < 2) {
                    month = "0" + month;
                }
                if (day.length() < 2) {
                    day = "0" + day;
                }
            }
            if (timeStr.length() < 8) {
                String[] timeSplit = timeStr.split(":");
                if (timeSplit[0].length() < 2) {
                    String newDate = year + "-" + month + "-" + day + " 0" + dateArr[1];
                    return convert(newDate);
                }
            }
            String newDate = year + "-" + month + "-" + day + " " + dateArr[1];
            return convert(newDate);
        } catch (Exception e) {
            return null;
        }
    }

    public static void main(String[] args) {
        String text = "2023/2/3 4:3:5";
        System.out.println(DateUtils.convertStringToLocalDateTime(text));
        BigDecimal one = new BigDecimal(-10);
        BigDecimal two = new BigDecimal(-10);
        System.out.println(one.subtract(two));
    }
}
